package com.zsl.swing.redis.desktop.window.dialog;

import com.zsl.swing.redis.desktop.model.NodeEntity;
import com.zsl.swing.redis.desktop.type.BtnOptEnum;
import com.zsl.swing.redis.desktop.type.UnitTypeEnum;
import com.zsl.swing.redis.desktop.utils.*;
import com.zsl.swing.redis.desktop.window.ZslRedisDesktopMainWindow;
import com.zsl.swing.redis.desktop.window.builder.ConfirmCancelBtnPanelBuilder;
import com.zsl.swing.redis.desktop.window.builder.UnitTypeComboBoxBuilder;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionListener;
import java.util.Objects;

public class ZslExpireTimeSetDialog extends JDialog {

    private final JTextField keyField;

    private final JTextField expField;

    private final JComboBox<String> unitField;

    private final NodeEntity dbEntity;

    public ZslExpireTimeSetDialog(String key, NodeEntity dbEntity){
        super(ZslRedisDesktopMainWindow.getMainWindow());
        this.dbEntity = dbEntity;
        this.keyField = new JTextField(30);
        this.expField = new JTextField(30);

        this.unitField = UnitTypeComboBoxBuilder.getInstance();

        this.initDialog(key);
    }

    public static void showDialog(String key, NodeEntity dbEntity){
        new ZslExpireTimeSetDialog(key, dbEntity);
    }

    private void initDialog(String key) {
        this.keyField.setText(key);
        this.keyField.setEditable(false);

        int width = 600,height = 400;
        this.setSize(width,height);

        this.addPanel();

        int x = CommonUtils.maxWidth()/2 - width/2;
        int y = CommonUtils.maxHeight()/2 - height/2;
        this.setLocation(x, y);

        this.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
        this.setVisible(true);
    }

    private void addPanel() {
        JPanel contentPanel = new JPanel(new GridBagLayout());

//        GridBagConstraints constraints = new GridBagConstraints();
//        constraints.anchor = GridBagConstraints.EAST;
//        constraints.fill = GridBagConstraints.HORIZONTAL;

        int labelWidth = 10, fieldWidth = 60, separator = 10,lineHeight = 10;
        GridBagUtils.getInstance()
                .add(new JLabel("key   :"),contentPanel,0,0, labelWidth, lineHeight)
                .add(this.keyField,contentPanel,labelWidth + separator,0, fieldWidth, lineHeight)
                .add(new JLabel("unit  :"),contentPanel,0,lineHeight + separator, labelWidth, lineHeight)
                .add(this.unitField,contentPanel,labelWidth + separator,lineHeight + separator, fieldWidth, lineHeight)
                .add(new JLabel("expire:"),contentPanel,0,(lineHeight + separator)*2, labelWidth, lineHeight)
                .add(this.expField,contentPanel,labelWidth + separator,(lineHeight + separator)*2, fieldWidth, lineHeight);

        Box hBox = Box.createVerticalBox();
        hBox.add(contentPanel);
        hBox.add(this.initBtnPanel());

        this.setContentPane(hBox);
    }

    private String getExpireTime(){
        String text = this.expField.getText();
        return StringUtils.isEmpty(text)?null:text.trim();
    }

    private String getUnit(){
        return (String) this.unitField.getSelectedItem();
    }

    private JPanel initBtnPanel(){
        ActionListener actionListener = event -> {
            String command = event.getActionCommand();
            if(BtnOptEnum.CANCEL.getText().equals(command)){
                this.dispose();
            }else if(BtnOptEnum.CONFIRM.getText().equals(command)){
                String expireStr = this.getExpireTime();
                if(StringUtils.isEmpty(expireStr)){
                    DialogUtils.errorDialog(ZslRedisDesktopMainWindow.getMainWindow(),"过期时间不能为空！");
                    return;
                }

                Integer expireTime;
                try{
                    expireTime = Integer.parseInt(this.expField.getText());

                    expireTime = UnitTypeEnum.toSeconds(this.getUnit(), expireTime);
                }catch (Exception e){
                    DialogUtils.errorDialog(ZslRedisDesktopMainWindow.getMainWindow(),"过期时间必须是数字！");
                    return;
                }

                if(Objects.isNull(expireTime)){
                    return;
                }

                long start = System.currentTimeMillis();
                boolean result = RedisUtils.expire(this.dbEntity.getUniqueId(),this.dbEntity.getDbIndex(),this.keyField.getText(), expireTime);
                ZslRedisDesktopMainWindow.getZslLogPanel().log("界面执行[expire]命令耗时：" + (System.currentTimeMillis() - start) + "秒！");
                if(result){
                    this.dispose();
                    DialogUtils.msgDialog(ZslRedisDesktopMainWindow.getMainWindow(),"设置过期时间成功！");
                }else{
                    DialogUtils.errorDialog(ZslRedisDesktopMainWindow.getMainWindow(),"设置过期时间失败！");
                }
            }
        };
        return ConfirmCancelBtnPanelBuilder.getInstance(actionListener);
    }

//    public void add(Component s,JPanel t,GridBagConstraints constraints,int x,int y,int w, int h) {
//        constraints.gridheight = h;
//        constraints.gridwidth = w;
//        constraints.gridx = x;
//        constraints.gridy = y;
//        t.add(s,constraints);
//    }
}
